/*
 * UrbanParksMenuBar.java
 */

package gui;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import model.ListManager;

/**
 * UrbanParksMenuBar is a JMenuBar that is a generic
 * JMenuBar for the Urban Parks program.
 * 
 * @author Sean Brown
 * @version 1.0
 */
@SuppressWarnings("serial")
public class UrbanParksMenuBar extends JMenuBar {

	/**
	 * The WelcomeFrame that started the program.
	 */
	private final WelcomeFrame my_welcome_frame;
	
	/**
	 * The JFrame that this menu bar is a part of.
	 */
	private final JFrame my_parent_frame;
	
	/**
	 * The ListManager to obtain data from.
	 */
	protected final ListManager my_lm;
  
  /**
   * Constructor initializes the menu bar and sets up its 
   * components.
   * 
   * @param the_frame - the frame that contains this menu bar.
   */
  public UrbanParksMenuBar(final WelcomeFrame the_login_frame, 
		  final JFrame the_parent_frame, final ListManager the_lm) {
    super();
    my_welcome_frame = the_login_frame;
    my_parent_frame = the_parent_frame;
    my_lm = the_lm;
    add(fileMenu());
  }
  
  /**
   * baseMenu method takes a String and converts it to a 
   * JMenu with a mnemonic for the first letter.
   * 
   * @param the_label - the name for the menu.
   * @return menu - the new JMenu for the given String.
   */
  protected JMenu baseMenu(final String the_label) {
    final JMenu menu = new JMenu(the_label);
    menu.setMnemonic(the_label.charAt(0));
    return menu;
  }
  
  /**
   * fileMenu method returns a menu containing all components
   * in the "File" menu.
   * 
   * @return file_menu - the "File" menu.
   */
  private JMenu fileMenu() {
    final JMenu file_menu = baseMenu("File");
    file_menu.add(new JMenuItem(new NewLogoutAction()));
    file_menu.addSeparator();
    file_menu.add(new JMenuItem(new ExitAction()));
    return file_menu;
  }
  
  /**
   * NewLogoutAction is an AbstractAction that defines the actions
   * of the "Logout" function.
   */
  private class NewLogoutAction extends AbstractAction {
    /**
     * Constructor that sets the title, mnemonic, description
     * and accelerator keys of the Action.
     */
    public NewLogoutAction() {
      super("Logout");
      putValue(SHORT_DESCRIPTION, "Return to the Welcome screen");
      putValue(MNEMONIC_KEY, Integer.valueOf('L'));
      putValue(ACCELERATOR_KEY, 
               KeyStroke.getKeyStroke('L',
                                      InputEvent.ALT_MASK));
    }
    
    @Override 
    public void actionPerformed(final ActionEvent the_event) {
		my_lm.switchUser(null);
		my_welcome_frame.exitProcedure(my_parent_frame);
		if (!my_parent_frame.isActive()) {
			my_welcome_frame.setVisible(true);
		}
    }
  }
  
  /**
   * ExitAction is an AbstractAction that defines the actions
   * of the "Exit" function.
   */
  private class ExitAction extends AbstractAction {

    /**
     * Constructor that sets the title, mnemonic, description
     * and accelerator keys of the Action.
     */
    public ExitAction() {
      super("Exit");
      putValue(SHORT_DESCRIPTION, "Exit the program");
      putValue(MNEMONIC_KEY, Integer.valueOf('X'));
      putValue(ACCELERATOR_KEY, 
               KeyStroke.getKeyStroke('X',
                                      InputEvent.ALT_MASK));
    }

    @Override
    public void actionPerformed(final ActionEvent the_event) {
    	my_welcome_frame.exitProcedure(my_parent_frame);
    }
  }
}


